﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Security;
using System.Threading;
using System.Windows.Forms;
using BlueMarineWindowsFormsClient.ManagerService;
using MarkupConverter;

namespace BlueMarineWindowsFormsClient
{
    public partial class frmHotelManager : Form
    {
        private const string Error = "ERROR: ";
        private const string Success = "SUCCESS: ";
        private const string Fail = "FAIL: ";
        private readonly frmMain parent;
        private ManagerServiceClient _hotel;

        public frmHotelManager(frmMain parent)
        {
            this.parent = parent;
            InitializeComponent();

            Temp_EquipmentDetails = new List<EquipmentDetail>();
            MinimumSize = Size;
            ptbPromotionBanner.SizeMode = PictureBoxSizeMode.Zoom;
            ptbPromotionImage.SizeMode = PictureBoxSizeMode.Zoom;
            InitServiceClient();
        }

        public List<EquipmentDetail> Temp_EquipmentDetails { get; set; }

        private void InitServiceClient()
        {
            _hotel = new ManagerServiceClient("WSHttpBinding_IManagerService")
                         {
                             ClientCredentials =
                                 {
                                     UserName = {UserName = parent.UName, Password = parent.PWord},
                                     ServiceCertificate =
                                         {
                                             Authentication =
                                                 {CertificateValidationMode = X509CertificateValidationMode.None}
                                         }
                                 }
                         };
        }

        private void AbortServiceClient()
        {
            if (_hotel.State != CommunicationState.Faulted)
            {
                _hotel.Abort();
            }
        }

        private void tabmain_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (hotelManager.SelectedIndex)
            {
                case 0:
                    ViewEquipment();
                    break;
                case 1:
                    ViewRoomType();
                    break;
                case 2:
                    ViewRoom();
                    FillCbxRoomType();
                    break;
                case 3:
                    ViewSupply();
                    break;
                case 4:
                    ViewPromotion();
                    break;
            }
        }

        //tab1

        private void RoomManagement_Load(object sender, EventArgs e)
        {
            ViewEquipment();
        }

        private void dtgEquipment_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgEquipment.CurrentRow != null)
                {
                    int id = int.Parse(dtgEquipment.Rows[dtgEquipment.CurrentRow.Index].Cells[0].Value.ToString());
                    EquipmentDetail equipmentDetail = _hotel.EquGetByID(id);
                    if (equipmentDetail == null) return;
                    txtEqID.Text = equipmentDetail.EquipmentDetailID.ToString(CultureInfo.InvariantCulture);
                    txtEqName.Text = equipmentDetail.EquipmentName;
                    txtEqPrice.Text = equipmentDetail.EquipmentPrice.ToString(CultureInfo.InvariantCulture);
                    chbxAvailableEquipment.Checked = equipmentDetail.Available;
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddEq_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Equipment...";
                if (_hotel.EquAdd(txtEqName.Text, decimal.Parse(txtEqPrice.Text),
                                  chbxAvailableEquipment.Checked))
                {
                    ViewEquipment();
                    lblStatus.Text = Success + "Create Equipment";
                }

                lblStatus.Text = Fail + " Create Equipment";
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void chbxViewAvaiEq_CheckedChanged(object sender, EventArgs e)
        {
            ViewEquipment();
        }

        private void btnUpdateEq_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Equipment...";
                if (_hotel.EquModify(int.Parse(txtEqID.Text), txtEqName.Text,
                                     decimal.Parse(txtEqPrice.Text),
                                     chbxAvailableEquipment.Checked))
                {
                    lblStatus.Text = "SUCCESS: Update Equipment";
                    ViewEquipment();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewEquipment()
        {
            try
            {
                List<EquipmentDetail> data = _hotel.EquGet(chbxViewAvaiEq.Checked);
                var source = new BindingSource {DataSource = data};
                dtgEquipment.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        //tab2

        private void dtgRoomType_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoomType.CurrentRow != null)
                {
                    int id = int.Parse(dtgRoomType.Rows[dtgRoomType.CurrentRow.Index].Cells[0].Value.ToString());
                    Temp_EquipmentDetails = _hotel.EquGetByRoomTypeID(id).ToList();
                    RoomType roomType = _hotel.RoTyGetByID(id);
                    txtRoTyID.Text = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                    txtRoTyTitle.Text = roomType.RoomTypeTitle;
                    txtRoTyRank.Text = roomType.RoomRank;
                    txtRoTyPrice.Text = roomType.RoomPrice.ToString(CultureInfo.InvariantCulture);
                    chbxBalcony.Checked = roomType.Balcony;
                    chbxBath.Checked = roomType.Bath;
                    chbxView.Checked = roomType.View;
                }

                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoTyEq_Click(object sender, EventArgs e)
        {
            var equipmentSelect = new frmEquipmentSelect(this);
            equipmentSelect.Show();
        }

        private void btnRemoveRoTyEq_Click(object sender, EventArgs e)
        {
            try
            {
                DataGridViewSelectedRowCollection idxs = dtgRoTyEq.SelectedRows;
                for (int i = 0; i < idxs.Count; i++)
                {
                    int idx = idxs[i].Index;
                    Temp_EquipmentDetails.RemoveAt(idx);
                }
                viewRoomTypeEquipment();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room Type...";
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }
                if (_hotel.RoTyAdd(txtRoTyTitle.Text, txtRoTyRank.Text,
                                   decimal.Parse(txtRoTyPrice.Text),
                                   chbxBath.Checked,
                                   chbxView.Checked, chbxBalcony.Checked, ids))
                {
                    lblStatus.Text = Success + "Create Room Type";
                    ViewRoomType();
                }
                lblStatus.Text = Fail + "Create Room Type";
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateRoomType_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room Type...";
                var ids = new List<int>();
                for (int i = 0; i < dtgRoTyEq.Rows.Count; i++)
                {
                    ids.Add(int.Parse(dtgRoTyEq.Rows[i].Cells[0].Value.ToString()));
                }


                if (_hotel.RoTyModify(int.Parse(txtRoTyID.Text), txtRoTyTitle.Text,
                                      txtRoTyRank.Text,
                                      decimal.Parse(txtRoTyPrice.Text),
                                      chbxBath.Checked, chbxView.Checked, chbxBalcony.Checked, ids))
                {
                    lblStatus.Text = Success + "Update Room Type";
                    ViewRoomType();
                    viewRoomTypeEquipment();
                }
                lblStatus.Text = Fail + "Update Room Type";
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        public void viewRoomTypeEquipment()
        {
            var source = new BindingSource {DataSource = Temp_EquipmentDetails};
            dtgRoTyEq.DataSource = source;
        }

        private void ViewRoomType()
        {
            List<RoomType> data = _hotel.RoTyGet();
            var source = new BindingSource {DataSource = data};
            dtgRoomType.DataSource = source;
        }

        //tab3

        private void FillCbxRoomType()
        {
            try
            {
                cbxRoomType.Items.Clear();
                List<RoomType> data = _hotel.RoTyGet();
                foreach (RoomType roomType in data)
                {
                    cbxRoomType.Items.Add(roomType);
                }
                cbxRoomType.DisplayMember = "RoomTypeTitle";
                cbxRoomType.ValueMember = "RoomTypeID";
                if (data.Count > 0)
                {
                    cbxRoomType.SelectedIndex = 0;
                }
            }
            catch (Exception ex)
            {
                AbortServiceClient();
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void dtgRoom_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgRoom.CurrentRow != null)
                {
                    int id = int.Parse(dtgRoom.Rows[dtgRoom.CurrentRow.Index].Cells[0].Value.ToString());
                    Room room = _hotel.RooGetByID(id);
                    txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                    switch (room.Status)
                    {
                        case 0:
                            radRoomOccupied.Checked = true;
                            break;
                        case 1:
                            radRoomAvailable.Checked = true;
                            break;
                        default:
                            radRoomUnavailable.Checked = true;
                            break;
                    }
                    for (int i = 0; i < cbxRoomType.Items.Count; i++)
                    {
                        var roomType = (RoomType) cbxRoomType.Items[i];
                        if (roomType.RoomTypeID == room.RoomTypeID)
                        {
                            cbxRoomType.SelectedIndex = i;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnAddRoom_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room...";
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                if (_hotel.RooAdd(int.Parse(txtRoomId.Text), int.Parse(state),
                                  int.Parse(roomTypeId)))
                {
                    lblStatus.Text = Success + "Create Room";
                    ViewRoom();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateRoom_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Room...";
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                string roomTypeId = roomType.RoomTypeID.ToString(CultureInfo.InvariantCulture);
                string state = "-1";
                if (!radRoomUnavailable.Checked)
                {
                    state = !radRoomOccupied.Checked ? "1" : "0";
                }
                if (_hotel.RooModify(int.Parse(txtRoomId.Text), int.Parse(state),
                                     int.Parse(roomTypeId)))
                {
                    lblStatus.Text = Success + "Update Room";
                    ViewRoom();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void frmRoomManager_Resize(object sender, EventArgs e)
        {
        }

        private void btnSearchStatus_Click(object sender, EventArgs e)
        {
            ViewRoom();
        }

        private void btnSearchEquipment_Click(object sender, EventArgs e)
        {
            try
            {
                List<EquipmentDetail> data = _hotel.EquGetByName(txtEqName.Text);
                var source = new BindingSource {DataSource = data};
                dtgEquipment.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = ex.Message;
            }
        }

        private void btnSeachByRoomTypeTitle_Click(object sender, EventArgs e)
        {
            try
            {
                var roomType = (RoomType) cbxRoomType.SelectedItem;
                int id = roomType.RoomTypeID;
                List<Room> data = _hotel.RooGetByRoTyID(id);
                var source = new BindingSource {DataSource = data};
                dtgRoom.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSeachRoom_Click(object sender, EventArgs e)
        {
            try
            {
                Room room = _hotel.RooGetByID(int.Parse(txtRoomId.Text));
                if (room == null)
                {
                    lblStatus.Text = string.Format("No room name {0}", txtRoomId.Text);
                    return;
                }
                txtRoomId.Text = room.RoomID.ToString(CultureInfo.InvariantCulture);
                switch (room.Status)
                {
                    case 0:
                        radRoomOccupied.Checked = true;
                        break;
                    case 1:
                        radRoomAvailable.Checked = true;
                        break;
                    default:
                        radRoomUnavailable.Checked = true;
                        break;
                }
                for (int i = 0; i < cbxRoomType.Items.Count; i++)
                {
                    var roomType = (RoomType) cbxRoomType.Items[i];
                    if (roomType.RoomTypeID == room.RoomTypeID)
                    {
                        cbxRoomType.SelectedIndex = i;
                    }
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewRoom()
        {
            try
            {
                int stt = 1;
                if (radRoomOccupied.Checked)
                {
                    stt = 0;
                }
                else if (radRoomUnavailable.Checked)
                {
                    stt = -1;
                }
                List<Room> data = _hotel.RooGet(stt);
                var source = new BindingSource {DataSource = data};
                dtgRoom.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        //Tab4

        private void btnAddSu_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Supply Detail...";
                if (_hotel.SupDetailAdd(chbxAvailableSupply.Checked, txtSupplyName.Text,
                                        decimal.Parse(txtSupplyPrice.Text), txtSupplyUnit.Text))
                {
                    lblStatus.Text = Success + "Create Supply Detail";
                    ViewSupply();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSearchSupply_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Searching Supply Detail...";
                List<SupplyDetail> data = _hotel.SupDetailGetByName(txtSupplyName.Text);
                var source = new BindingSource {DataSource = data};
                dtgSupply.DataSource = source;
                lblStatus.Text = "SUCCESS: Search Supply Detail...";
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdateSu_Click(object sender, EventArgs e)
        {
            try
            {
                lblStatus.Text = "Writting Supply Detail...";
                if (_hotel.SupDetailModify(int.Parse(txtSupplyId.Text), chbxAvailableSupply.Checked,
                                           txtSupplyName.Text, decimal.Parse(txtSupplyPrice.Text),
                                           txtSupplyUnit.Text))
                {
                    lblStatus.Text = Success + "Create Supply Detail";
                    ViewSupply();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void chbxViewAvaiSu_CheckedChanged(object sender, EventArgs e)
        {
            ViewSupply();
        }

        private void dtgSupply_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgSupply.CurrentRow != null)
                {
                    SupplyDetail supplyDetail =
                        _hotel.SupDetailGetById(
                            int.Parse(dtgSupply.Rows[dtgSupply.CurrentRow.Index].Cells[0].Value.ToString()));
                    txtSupplyName.Text = supplyDetail.SupplyName;
                    txtSupplyId.Text = supplyDetail.SupplyDetailID.ToString();
                    txtSupplyPrice.Text = supplyDetail.SupplyPrice.ToString();
                    txtSupplyUnit.Text = supplyDetail.Unit;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ViewSupply()
        {
            try
            {
                List<SupplyDetail> data = _hotel.SupDetailGet(chbxViewAvaiSu.Checked);
                var source = new BindingSource {DataSource = data};
                dtgSupply.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }


        //tab5

        private void ViewPromotion()
        {
            try
            {
                List<vSubPromotion> data = _hotel.ProGet(chbxAvailablePromotion.Checked);
                var source = new BindingSource {DataSource = data};
                dtgPromotion.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private Byte[] ImageToByte(String imagePath)
        {
            using (var FS = new FileStream(imagePath, FileMode.Open, FileAccess.Read))
            {
                var img = new byte[FS.Length];
                FS.Read(img, 0, Convert.ToInt32(FS.Length));
                return img;
            }
        }

        private Image ByteToImage(byte[] data)
        {
            try
            {
                var ms = new MemoryStream(data);
                Image image = Image.FromStream(ms);
                return image;
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
                return null;
            }
        }

        private void dtgPromotion_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgPromotion.CurrentRow != null)
                {
                    int promotionId =
                        int.Parse(dtgPromotion.Rows[dtgPromotion.CurrentRow.Index].Cells[0].Value.ToString());
                    lblPromotionId.Text = promotionId.ToString();
                    Promotion promotion = _hotel.ProGetById(promotionId);
                    rtxDescription.Rtf = HtmlToRtfConverter.ConvertHtmlToRtf(promotion.PromotionDescription);
                    txtPromotionTitle.Text = promotion.PromotionTitle;
                    dtpStart.Value = promotion.PromotionStart;
                    dtpEnd.Value = promotion.PromotionEnd;

                    ptbPromotionBanner.Image = ByteToImage(promotion.PromotionBanner);
                    txtPromotionBanner.Text = "";

                    ViewPromotionDetail();
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void ptbPromotionBanner_Click(object sender, EventArgs e)
        {
            try
            {
                var ofd = new OpenFileDialog {Filter = "JPG file|*.jpg|JPEG file|*.jpeg"};
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Image img = Image.FromFile(ofd.FileName);
                    ptbPromotionBanner.Image = img;
                    txtPromotionBanner.Text = ofd.FileName;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }

        private void ptbPromotionImage_Click(object sender, EventArgs e)
        {
            try
            {
                var ofd = new OpenFileDialog {Filter = "JPG file|*.jpg|JPEG file|*.jpeg"};
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    Image img = Image.FromFile(ofd.FileName);
                    ptbPromotionImage.Image = img;
                    txtPromotionImage.Text = ofd.FileName;
                    ptbPromotionImage.SizeMode = PictureBoxSizeMode.Zoom;
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }

        private void btbAddPromotion_Click(object sender, EventArgs e)
        {
            try
            {
                string decr = RtfToHtmlConverter.ConvertRtfToHtml(rtxDescription.Rtf);
                byte[] banner = ImageToByte(txtPromotionBanner.Text);
                if (_hotel.ProAdd(dtpStart.Value, dtpEnd.Value, txtPromotionTitle.Text, decr, banner))
                {
                    lblStatus.Text = Success + "Add Promotion";
                    ViewPromotion();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }

        private void btnDeletePromotion_Click(object sender, EventArgs e)
        {
            try
            {
                if (_hotel.ProRemove(int.Parse(lblPromotionId.Text)))
                {
                    lblStatus.Text = Success + "Delete Promotion";
                    ViewPromotion();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }

        private void btnUpdatePromotion_Click(object sender, EventArgs e)
        {
            try
            {
                string decr = RtfToHtmlConverter.ConvertRtfToHtml(rtxDescription.Rtf);
                byte[] banner = null;
                if (txtPromotionBanner.Text.Trim() != "")
                    banner = ImageToByte(txtPromotionBanner.Text);
                if (_hotel.ProModify(int.Parse(lblPromotionId.Text), dtpStart.Value, dtpEnd.Value,
                                     txtPromotionTitle.Text, decr, banner))
                {
                    lblStatus.Text = Success + "Update Promotion";
                    ViewPromotion();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }

        private void btnAddPromotionDetail_Click(object sender, EventArgs e)
        {
            try
            {
                string content = RtfToHtmlConverter.ConvertRtfToHtml(rtxPromotionContent.Rtf);
                byte[] image = ImageToByte(txtPromotionImage.Text);
                if (_hotel.ProDetailAdd(int.Parse(lblPromotionId.Text), content, image))
                {
                    lblStatus.Text = Success + "Add Promotion Detail";
                    ViewPromotionDetail();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = "ERROR: " + ex.Message;
            }
        }


        private void ViewPromotionDetail()
        {
            try
            {
                List<vSubPromotionDetail> data = _hotel.ProDetailGetByProId(int.Parse(lblPromotionId.Text));
                var source = new BindingSource {DataSource = data};
                dtgPromotionDetail.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void dtgPromotionDetail_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                if (dtgPromotionDetail.CurrentRow != null)
                {
                    int promotionDetailId =
                        int.Parse(dtgPromotionDetail.Rows[dtgPromotionDetail.CurrentRow.Index].Cells[0].Value.ToString());
                    lblPromotionDetailId.Text = promotionDetailId.ToString();
                    PromotionDetail promotionDetail = _hotel.ProDetailGetById(promotionDetailId);
                    rtxPromotionContent.Rtf = HtmlToRtfConverter.ConvertHtmlToRtf(promotionDetail.PromotionContent);

                    ptbPromotionImage.Image = ByteToImage(promotionDetail.PromotionImage);
                    txtPromotionImage.Text = "";
                }
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnUpdatePromotinDetail_Click(object sender, EventArgs e)
        {
            try
            {
                string content = RtfToHtmlConverter.ConvertRtfToHtml(rtxPromotionContent.Rtf);
                byte[] image = null;
                if (txtPromotionImage.Text.Trim() != "")
                {
                    image = ImageToByte(txtPromotionImage.Text);
                }
                if (_hotel.ProDetailModify(int.Parse(lblPromotionDetailId.Text), content, image))
                {
                    lblStatus.Text = Success + "Update Promotion Detail";
                    ViewPromotionDetail();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnDeletePromotionDetail_Click(object sender, EventArgs e)
        {
            try
            {
                if (_hotel.ProDetailRemove(int.Parse(lblPromotionDetailId.Text)))
                {
                    lblStatus.Text = Success + "Delete Promotion Detail";
                    ViewPromotionDetail();
                }
            }
            catch (FaultException)
            {
                AbortServiceClient();
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSearchPromotionTitle_Click(object sender, EventArgs e)
        {
            try
            {
                List<vSubPromotion> data = _hotel.ProGetByTitle(txtPromotionTitle.Text);
                var source = new BindingSource {DataSource = data};
                dtgPromotion.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void btnSearchPromotionDate_Click(object sender, EventArgs e)
        {
            try
            {
                List<vSubPromotion> data = _hotel.ProGetByTime(dtpStart.Value, dtpEnd.Value);
                var source = new BindingSource {DataSource = data};
                dtgPromotion.DataSource = source;
            }
            catch (Exception ex)
            {
                lblStatus.Text = Error + ex.Message;
            }
        }

        private void frmHotelManager_FormClosed(object sender, FormClosedEventArgs e)
        {
            parent.Dispose();
        }

        private void employeeManagerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (parent.Emp == null)
            {
                parent.Emp = new frmEmployeeManager(parent);
                parent.Emp.Show();
                Hide();
            }
            else
            {
                parent.Emp.Show();
                Hide();
            }
        }

        private void logoutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var t = new Thread(ThreadProc);
            t.Start();
            Close();
        }

        public void ThreadProc()
        {
            Application.Run(new frmMain(parent.UName, ""));
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}